package com.korion.srceencapture;


import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;


/**
 * @author chenyinying
 * @date 2020/9/28
 */
public class CommandExec {

    private static final String TAG = "CommandExec";

    public static void screenShot(Context context, int width, int height){
        String packageName = context.getPackageName();
        PackageManager pkgMgr = context.getPackageManager();
        String dir = null;
        try {
            ApplicationInfo info = pkgMgr.getApplicationInfo(packageName, 0);
            dir = info.publicSourceDir;
//            Log.d(TAG, "dir:"+dir);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (dir != null) {
            File workSpace = context.getExternalFilesDir(null);
            String cmd = "app_process -Djava.class.path="+dir+" "+workSpace.getAbsolutePath()+" "+SurfaceControlProxy.class.getCanonicalName()+" "+workSpace.getAbsolutePath();
            try {
                Process process = Runtime.getRuntime().exec(cmd);
                InputStream input = process.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                String line;
                do {
                    line = reader.readLine();
                    if (line != null) {
                        Log.d(TAG, line);
                    }
                }while (line != null);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
